<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // class People {
        //     constructor(name, age) {
        //         this.name = name
        //         this.age = age
        //     }

        //     say () {
        //         console.log('i can say')
        //     }

        //     static eat () {
        //         console.log(' 只能父类调用,子类不能调用')
        //     }
        // }


        function People (name, age) {
            "use strict"
            var name = (arguments.length > 0 && arguments[0] !== undefined) ? arguments[0] : name
            var age = (arguments.length > 1 && arguments[1] !== undefined) ? arguments[1] : age
            classCheck(this, People)
            this.name = name
            this.age = age
        }

        var createClass = (function () {
            function defineProperties (constructor, props) {
                for (var i = 0; i < props.length; i++) {
                    var descriptor = props[i]
                    descriptor.enumerable = descriptor.enumerable || false
                    descriptor.configurable = descriptor.configurable || true
                    Object.defineProperty(constructor, descriptor.key, descriptor)
                }
            }
            return function (constructor, protoProps, staticProps) {
                if (protoProps) defineProperties(constructor.prototype, protoProps)
                if (staticProps) defineProperties(constructor, staticProps)
            }
        })()

        createClass(People, [
            {
                key: 'say',
                value: function () {
                    console.log('i can say')
                }
            },
        ],
            [
                {
                    key: 'eat',
                    value: function () {
                        console.log(' 只能父类调用,子类不能调用')
                    }
                },
            ],
        )

        function classCheck (instance, constructor) {
            // if (typeof new.target === undefined) throw new Error('只能通过new执行')
            // 或者
            if (!(instance instanceof constructor)) throw new Error('只能通过new执行')
        }

        var p = new People('xxx', 18)
        console.log(p)
        console.log(p.say())
        console.log(People.eat())

        for (key in p) {
            console.log(key)
        }
    </script>
</body>

</html>